package com.yanqu.road.dao.impl.activity.ad;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ad.AdActivityUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class AdUserDataDaoImpl extends TempDataObjectDao<AdActivityUserData> {

    public AdActivityUserData getAdActivityUserData(int activityId, long userId) {
        String sql = "select * from `t_u_activity_ad_user_data` where `activityId`=? and `userId`=?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, activityId);
            call.setLong(2, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean updateData(AdActivityUserData data) {
        String sql = "update `t_u_activity_ad_user_data` set dailyWatchTimes=?,firstTotalWatchTimes=?,firstRewardFinish=?," +
                "secondTotalWatchTimes=?,dailyResetTime=?,lastWatchTime=?,firstRewardData=?,secondRewardData=?,getUnlockReward=?" +
                " where activityId=? and userId=?";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getDailyWatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFirstTotalWatchTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isFirstRewardFinish()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSecondTotalWatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDailyResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastWatchTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getFirstRewardList(), ",")));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getSecondRewardList(), ",")));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetUnlockReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean addData(AdActivityUserData data) {
        String sql = "insert into `t_u_activity_ad_user_data`(activityId,userId,dailyWatchTimes,firstTotalWatchTimes,firstRewardFinish," +
                "secondTotalWatchTimes,dailyResetTime,lastWatchTime,firstRewardData,secondRewardData,getUnlockReward) " +
                "values (?,?,?,?,?,?,?,?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDailyWatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFirstTotalWatchTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isFirstRewardFinish()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSecondTotalWatchTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDailyResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastWatchTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getFirstRewardList(), ",")));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getSecondRewardList(), ",")));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetUnlockReward()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected AdActivityUserData parseData(ResultSet rs) throws SQLException {
        AdActivityUserData data = new AdActivityUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setDailyWatchTimes(rs.getInt("dailyWatchTimes"));
        data.setFirstTotalWatchTimes(rs.getLong("firstTotalWatchTimes"));
        data.setFirstRewardFinish(rs.getBoolean("firstRewardFinish"));
        data.setSecondTotalWatchTimes(rs.getLong("secondTotalWatchTimes"));
        data.setDailyResetTime(rs.getLong("dailyResetTime"));
        data.setLastWatchTime(rs.getLong("lastWatchTime"));
        data.setFirstRewardList(StringUtils.stringToIntegerList(rs.getString("firstRewardData"), ","));
        data.setSecondRewardList(StringUtils.stringToIntegerList(rs.getString("secondRewardData"), ","));
        data.setGetUnlockReward(rs.getBoolean("getUnlockReward"));
        return data;
    }
}
